package com.javasm.controller.goodsController;

import com.javasm.common.http.AxiosResult;
import com.javasm.common.page.PageResult;
import com.javasm.controller.base.BaseController;
import com.javasm.domin.criteria.goodsCriteria.GoodsCriteria;
import com.javasm.domin.criteria.goodsCriteria.UpdateStockNumCrireria;
import com.javasm.domin.entity.goodsEntity.GdGoods;
import com.javasm.domin.vo.goodsVo.GoodsVo;
import com.javasm.service.goodsService.GdGoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author：chenliying
 * @Version：1.0
 * @Date：2021/11/18-22:02
 * @Since:jdk1.8
 * @Description:
 */
@RestController
@RequestMapping("/goods")
@RequiredArgsConstructor
public class GoodsController extends BaseController {

    private final GdGoodsService gdGoodsService;

    /**
     * 查询所有
     * @param goodsCriteria
     * @return
     */
    @GetMapping("findList")
    public AxiosResult<PageResult<GoodsVo>> list(GoodsCriteria goodsCriteria){
        PageResult<GoodsVo> goodsVoPageResult = gdGoodsService.searchPage(goodsCriteria);
//        System.out.println(goodsVoPageResult);
        return AxiosResult.success(goodsVoPageResult);
    }

    /**
     * 根据id查寻
     * @param id
     * @return
     */
    @GetMapping("findById/{id}")
    public AxiosResult<GdGoods>findById(@PathVariable Long id){

        GdGoods byId = gdGoodsService.getById(id);
        return AxiosResult.success(byId);
    }

    /**
     * 添加
     * @param gdGoods
     * @return
     */
    @PostMapping("addGoods")
    public AxiosResult save(@RequestBody GdGoods gdGoods){
        if(gdGoods.getGoodsId()==null){
            gdGoods.setGoodsId("132661302");
        }
        gdGoods.setId((Integer) null);
        System.out.println("==============="+gdGoods.toString());
        boolean save = gdGoodsService.save(gdGoods);
        System.out.println(save);
        return AxiosResult.success(save);
    }

    /**
     * 根据id修改
     * @param gdGoods
     * @return
     */
    @PutMapping("updateGoods")
    public AxiosResult update(@RequestBody GdGoods gdGoods){
//        System.out.println(gdGoods.toString());
        System.out.println("========================"+gdGoods.getId());
        return AxiosResult.success(gdGoodsService.updateById(gdGoods));
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteById/{id}")
    public AxiosResult<Boolean> delete(@PathVariable Long id){
        return AxiosResult.success(gdGoodsService.removeById(id));
    }

    @DeleteMapping("goods/{ids}")
    public AxiosResult<Void> batchDelete(@PathVariable List<Long> ids){
        return toAxios(gdGoodsService.batchDeleteByIds(ids));
    }



//    @PutMapping("updateByIdStock")
//    public AxiosResult<Void> updateByIdStock(@RequestBody UpdateStockNumCrireria updateStockNumCrireria){
//
//
//        System.out.println("================"+updateStockNumCrireria.getId());
//        System.out.println("================"+updateStockNumCrireria.getStock());
//        return toAxios(gdGoodsService.updateByIdStock(updateStockNumCrireria.getId(),updateStockNumCrireria.getStock()));
//    }

    @PutMapping("updateByIdStock")
    public AxiosResult<Void> updateByIdStock(Long id,Long stock){


        System.out.println("================"+id);
        System.out.println("================"+stock);
        return toAxios(gdGoodsService.updateByIdStock(id,stock));
    }


}
